package com.example.administrator.myapplication;

import android.os.Build;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.SurfaceView;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import android.view.WindowManager;
import android.widget.Button;

import com.baidu.recorder.api.LiveConfig;
import com.baidu.recorder.api.LiveSession;
import com.baidu.recorder.api.LiveSessionHW;
import com.baidu.recorder.api.LiveSessionSW;
import com.baidu.recorder.api.SessionStateListener;

public class MainActivity extends AppCompatActivity {
    private SurfaceView sfv;
    private Button btn;
    private LiveSession ls;

    public void btnClick(View view){
        if(btn.getText().equals("开始直播")){
            ls.startRtmpSession("rtmp://192.168.25.15.1935/live/xiaowang");
            btn.setText("停止直播");
        }else {
            ls.stopRtmpSession();
            btn.setText("开始直播");
        }
    }


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setContentView(R.layout.activity_main);
        getSupportActionBar().hide();
        sfv= (SurfaceView) findViewById(R.id.sfv);
        btn= (Button) findViewById(R.id.btn);
        initRTMP();


    }

    private void initRTMP() {
        //第一步定义LiveConfig
        LiveConfig liveConfig=new LiveConfig.Builder()
                .setCameraId(LiveConfig.CAMERA_FACING_FRONT)//设置为前摄像头
                .setCameraOrientation(LiveConfig.ORIENTATION_PORTRAIT)//设置为竖屏
                .setVideoWidth(1280)
                .setVideoHeight(720)
                .setVideoFPS(15)
                .setInitVideoBitrate(1200000)
                .setAudioBitrate(64*1000)
                .setAudioSampleRate(LiveConfig.AUDIO_SAMPLE_RATE_44100)
                .setGopLengthInSeconds(2)
                .build();
        if(Build.VERSION.SDK_INT>=18){
            //硬编码
            ls=new LiveSessionHW(this,liveConfig);
        }else {
            //软编码
            ls=new LiveSessionSW(this,liveConfig);
        }
        ls.setStateListener(new SessionStateListener() {
            @Override
            public void onSessionPrepared(int i) {

            }

            @Override
            public void onSessionStarted(int i) {
            //启动
            }

            @Override
            public void onSessionStopped(int i) {

            }

            @Override
            public void onSessionError(int i) {

            }
        });
        ls.bindPreviewDisplay(sfv.getHolder());
        ls.prepareSessionAsync();

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}
